package com.school.aftersalemanagement.controller.ui.maintenancerecord;

import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;

import com.school.aftersalemanagement.R;
import com.school.aftersalemanagement.controller.adapter.MaintenanceRecordAdapter;
import com.school.aftersalemanagement.entity.Record;
import com.school.aftersalemanagement.global.BaseToolBarActivity;
import com.school.aftersalemanagement.service.BicycleService;
import com.school.aftersalemanagement.service.impl.BicycleServiceimpl;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MaintenanceRecordActivity extends BaseToolBarActivity {

    @BindView(R.id.maintenanceRecordList)
    ListView maintenanceRecordList;
    private MaintenanceRecordAdapter maintenanceRecordAdapter;
    private BicycleService bicycleService;
    private List<Record> records;
    private int GETMAINTANCERECORD = 1;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == GETMAINTANCERECORD) {
                List<Record> record = (List<Record>)msg.obj;
                records.addAll(record);
                if(records.size()>=1) {
                    maintenanceRecordAdapter.notifyDataSetChanged();
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintenancerecord);
        setToolbarText("保养详情");
        View headerView = LayoutInflater.from(this).inflate(R.layout.adapter_maintenancerecord,null);
        //可以添加多个HeaderView
        maintenanceRecordList.addHeaderView(headerView);
        records = new ArrayList<>();
        maintenanceRecordAdapter = new MaintenanceRecordAdapter(this,records);
        maintenanceRecordList.setAdapter(maintenanceRecordAdapter);
        bicycleService = new BicycleServiceimpl(this,mHandler);
        bicycleService.searchMaintance(GETMAINTANCERECORD);
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }
}
